package com.mihu.stmall.manager.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mihu.stmall.manager.entiy.Goods;
import com.mihu.stmall.manager.entiy.GoodsPic;
import com.mihu.stmall.manager.mapper.GoodsMapper;
import com.mihu.stmall.manager.service.GoodsPicService;
import com.mihu.stmall.manager.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

//实现类 实现servce层接口  添加service注解
//添加service注解
@Service("GoodsService")
public class GoodsServiceImpl implements GoodsService {

    //自动注入mapper层对象
    @Resource
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsPicService goodsPicService;

    @Override
    //开启Spring事务
    @Transactional(
            isolation = Isolation.SERIALIZABLE,// ACID 隔离级别
            propagation = Propagation.REQUIRED,//传播机制
            rollbackFor = Exception.class//当方法中抛出任何异常时 回滚事务
    )
    public int add(Goods goods) throws Exception {
        //验证参数
        if (goods.getName()==null
                ||goods.getName().length()==0){
            throw new Exception("商品名不能为空");
        }
        if (goods.getDscp()==null
        ||goods.getDscp().length()==0){
            throw new Exception("描述不能为空");
        }
        if (goods.getPrice()==null ||
        goods.getMarkPrice()==null){
            throw new Exception("商品价格或市场价不能为空");
        }
        if (goods.getColor()==null ||
        goods.getColor().length()==0){
            throw new Exception("商品颜色不能为空");
        }
        if (goods.getVersion()==null ||
        goods.getVersion().length()==0){
            throw new Exception("商品版本不能为空");
        }
        if (goods.getContent()==null){
            throw new Exception("商品数量不能为空");
        }
        if (goods.getCategoryId()==null
        ){
            throw new Exception("商品分类不能为空");
        }
        if (goods.getRecom()==null){
            throw new Exception("推荐不能为空");
        }
//        逻辑处理
//          先保存商品的数据   然后还要保存图片的数据（需要商品的id 获取id主键）
      int code = goodsMapper.insert(goods);

        if (code >0){
            //保存图片
            for (GoodsPic pic:goods.getPics()
                 ) {
                pic.setGoodsId(goods.getId());
            }

            goodsPicService.addPatch(goods.getPics());

        }

//        访问数据库
       return code;
    }

    @Override
    public int remove(Integer id) throws Exception {
        Goods goods = goodsMapper.selectById(id);
        if (goods==null){
            throw new Exception("ID不存在");
        }
        //只会删除goods不会删除图片
        return goodsMapper.delete(id);
    }


    @Override
    //开启Spring事务
    @Transactional(
            isolation = Isolation.SERIALIZABLE,// ACID 隔离级别
            propagation = Propagation.REQUIRED,//传播机制
            rollbackFor = Exception.class//当方法中抛出任何异常时 回滚事务
    )
    public int edit(Goods goods) throws Exception {
        Goods sgoods = goodsMapper.selectById(goods.getId());
        if (sgoods==null){
            throw new Exception("ID不存在");

        }
        int code = goodsMapper.update(goods);
        if (goods.getPics() !=null) {
            //保存图片
            for (GoodsPic pic : goods.getPics()
            ) {
                pic.setGoodsId(goods.getId());
            }
            goodsPicService.removeByGoodsId(goods.getId());
            goodsPicService.addPatch(goods.getPics());


            //同样 修改了goods 图片未修改

        }
        return code;
    }

    @Override
    public Goods getById(Integer id) throws Exception {

        Goods goods = goodsMapper.selectById(id);
        if (goods == null){
            throw new Exception("ID不存在");
        }
        return goodsMapper.selectById(id);
    }

    @Override
    public List<Goods> search(Goods goods) {
        return goodsMapper.select(goods);
    }

    @Override
    public PageInfo searchByPage(Integer page, Integer limit, Goods goods) {
        //分三步
        //1 启动分页
        PageHelper.startPage(page,limit);
        //2 调用Mapper层
        List<Goods> list = goodsMapper.select(goods);
        //3 构造PageInfo
        PageInfo pageInfo = new PageInfo(list);

        return pageInfo;
    }
}
